package cn.com.cifi.mars.service.impl;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;
import java.util.stream.Collectors;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.com.cifi.mars.bean.vo.supply.SumValueVO;
import cn.com.cifi.mars.constant.IsDeleteEnum;
import cn.com.cifi.mars.constant.OrgTypeEunm;
import cn.com.cifi.mars.constant.SalyTypeEnum;
import cn.com.cifi.mars.constant.VersionTypeEunm;
import cn.com.cifi.mars.entity.SupplyPlanVersionDetail;
import cn.com.cifi.mars.mapper.mars.SupplyPlanVersionDetailMapper;
import cn.com.cifi.mars.service.SupplyPlanVersionDetailService;
import cn.com.cifi.mars.util.LocalDateTimeUtils;
import cn.com.cifi.mars.util.NumberUtils;
import cn.com.cifi.mars.util.StrUtils;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 供货计划货值表 服务实现类
 * </p>
 *
 * @author yyd
 * @since 2019-09-11
 */
@Slf4j
@Service
public class SupplyPlanVersionDetailServiceImpl extends ServiceImpl<SupplyPlanVersionDetailMapper, SupplyPlanVersionDetail> implements SupplyPlanVersionDetailService {

	@Autowired
	private SupplyPlanVersionDetailMapper supplyPlanVersionDetailMapper;
	
	List<SupplyPlanVersionDetail> parentList = null;
	
	@Override
	public List<SupplyPlanVersionDetail> queryByVersionId(String supplyVersionId) {
		return supplyPlanVersionDetailMapper.selectByVersionId(supplyVersionId,IsDeleteEnum.NO.getKey());
	}

	@Override
	public List<Map<String, Object>> queryYears(String projectId) {
		return supplyPlanVersionDetailMapper.selectYears(projectId,IsDeleteEnum.NO.getKey());
	}

	@Override
	public synchronized List<SupplyPlanVersionDetail> collectSupplyPlanVersionDetailList(List<SupplyPlanVersionDetail> AllList,String versionType, String versionId,String code, String year, String month) {
		if(StrUtils.isNotEmpty(month)) {
			year = year+"-"+month;
		}
	    code = StrUtils.prepareCondition(code);
	    List<SupplyPlanVersionDetail> list = getTempList(AllList);
		List<SupplyPlanVersionDetail> dataList = Collections.synchronizedList(new ArrayList<>());
		List<SupplyPlanVersionDetail> resdataList = Collections.synchronizedList(new ArrayList<>());
		List<SupplyPlanVersionDetail> lastList = Collections.synchronizedList(new ArrayList<>());
		//根据层级code获取全路劲
		List<SupplyPlanVersionDetail> fullPathList = supplyPlanVersionDetailMapper.selectOrgFullPath(versionId,code,year,month,IsDeleteEnum.NO.getKey());
		if(StrUtils.isNotEmpty(code) || StrUtils.isNotEmpty(year)) {
			for(SupplyPlanVersionDetail versionDetail : fullPathList) {
				parentList =  Collections.synchronizedList(new ArrayList<>());
				String orgFullPath = versionDetail.getOrgFullPathName();
				parentList = getParentList(list,versionDetail.getPid());
				List<SupplyPlanVersionDetail> childList = getSelfAndChildData(list,orgFullPath);
				dataList.addAll(parentList);
				dataList.addAll(childList);
			}
			//去重
			resdataList = dataList.stream().collect(
	                Collectors.collectingAndThen(
	                        Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(SupplyPlanVersionDetail::getId))), ArrayList::new)
	        );
			//处理提前供货数据
			handleData(resdataList,year,month);
		}else {
			resdataList.addAll(fullPathList);
		}
		//处理显示数据（经营方式为可租和不可租售数据不显示）
		handleShowData(resdataList,lastList);
		//清空存在子级的数据
		emptyData(lastList);
		//数据汇总
		collectData(lastList,versionType);
		//处理楼栋层级月度提前供货数据
		return handleBuildData(lastList,year,month);
	}

	private List<SupplyPlanVersionDetail> handleBuildData(List<SupplyPlanVersionDetail> dataList,String year,String month) {
		if(StrUtils.isNotEmpty(month)) {
			if(CollectionUtils.isEmpty(dataList)) {
				return dataList;
			}
			List<SupplyPlanVersionDetail> lastList = getTempList(dataList);
			month = year;
			List<String> monthOverIds = new ArrayList<>();
			String versionId = "";
			 List<SupplyPlanVersionDetail> list = Collections.synchronizedList(new ArrayList<>());
			for(SupplyPlanVersionDetail obj : lastList) {
				LocalDateTime supplyDateActual = obj.getSupplyTimeVirtual() == null ? obj.getSupplyDateActual() : obj.getSupplyTimeVirtual();
				if(supplyDateActual != null) {
					int yearMonth = LocalDateTimeUtils.toIntYearMonth(Integer.parseInt(month.split("-")[0]), Integer.parseInt(month.split("-")[1]));
					int actualYearMonth = LocalDateTimeUtils.toIntYearMonth(supplyDateActual.getYear(),supplyDateActual.getMonthValue());
					if(actualYearMonth < yearMonth) {
						versionId = obj.getVersionId();
						monthOverIds.add(obj.getOrgFullPathName());
					}
				}
			}
			if(CollectionUtils.isNotEmpty(monthOverIds)) {
				list = supplyPlanVersionDetailMapper.selectByVersionId(versionId, IsDeleteEnum.NO.getKey());
			}
			
			for(String str : monthOverIds) { 
				List<SupplyPlanVersionDetail> childList = getSelfAndChildData(lastList,str);
				for(SupplyPlanVersionDetail childObj : childList) {
					for(SupplyPlanVersionDetail resData : lastList) {
						if(resData.getId().equals(childObj.getId())) {
							SupplyPlanVersionDetail obj  = getSupplyPlanVersionDetail(list,resData.getId()); 
							if(obj != null) {
								if(obj.getIsParking() != null && obj.getIsParking().intValue() == 1) {
									resData.setNums(obj.getParkingNum()+"");
									resData.setPrice(obj.getNotCommodityValue()+"");
									resData.setAvgPrice(StrUtils.divide(obj.getNotCommodityValue()+"",obj.getParkingNum()+"",2));
								}else {
									resData.setNums(obj.getRoomNum()+"");
									resData.setPrice(obj.getHouseCommodityValue()+"");
									resData.setSaleArea(obj.getSaleArea());
									resData.setAvgPrice(StrUtils.divide(obj.getHouseCommodityValue()+"",obj.getSaleArea()+"",2));
								}
							}
						}
					}
				}
			}
			return lastList;
		}
		return dataList;
	}

	private SupplyPlanVersionDetail getSupplyPlanVersionDetail(List<SupplyPlanVersionDetail> list, String id) {
		if(CollectionUtils.isNotEmpty(list)) {
			for(SupplyPlanVersionDetail detail : list) {
				if(detail.getId().equals(id)) {
					return detail;
				}
			}
		}
		return null;
	}

	private void handleData(List<SupplyPlanVersionDetail> resdataList,String year,String month) {
		if(StrUtils.isNotEmpty(month)) {
			month=year;
			year = "";
		}
		List<String> planIds = new ArrayList<>();
		List<String> actualIds = new ArrayList<>();
		List<String> yearOverIds = new ArrayList<>();
		List<String> monthOverIds = new ArrayList<>();
		for(SupplyPlanVersionDetail obj : resdataList) {
				LocalDateTime supplyDate = obj.getSupplyDate();
				LocalDateTime supplyDateActual = obj.getSupplyTimeVirtual() == null ? obj.getSupplyDateActual() : obj.getSupplyTimeVirtual();
				if(supplyDate != null) {
					if(StrUtils.isNotEmpty(year)) {
						String yearStr = String.valueOf(supplyDate.getYear());
						if(!yearStr.equals(year)) {
							planIds.add(obj.getId());
						}
					}
					if(StrUtils.isNotEmpty(month)) {
						String monthStr = LocalDateTimeUtils.formatTime(supplyDate, "yyyy-MM");
						if(!monthStr.equals(month)) {
							planIds.add(obj.getId());
						}
					}
				}
				
				if(supplyDateActual != null) {
					if(StrUtils.isNotEmpty(year)) {
						String yearStr = String.valueOf(supplyDateActual.getYear());
						if(!yearStr.equals(year)) {
							actualIds.add(obj.getId());
						}
						if(supplyDateActual.getYear() < Integer.parseInt(year)) {
							yearOverIds.add(obj.getId());
						}
					}
					if(StrUtils.isNotEmpty(month)) {
						String monthStr = LocalDateTimeUtils.formatTime(supplyDateActual, "yyyy-MM");
						if(!monthStr.equals(month)) {
							actualIds.add(obj.getId());
						}
						
						int yearMonth = LocalDateTimeUtils.toIntYearMonth(Integer.parseInt(month.split("-")[0]), Integer.parseInt(month.split("-")[1]));
						int actualYearMonth = LocalDateTimeUtils.toIntYearMonth(supplyDateActual.getYear(),supplyDateActual.getMonthValue());
						if(actualYearMonth < yearMonth) {
							monthOverIds.add(obj.getId());
						}
					}
				}
		}		
		
		for(String str : planIds) {
			List<SupplyPlanVersionDetail> list = getChildData(resdataList,str);
			for(SupplyPlanVersionDetail obj : list) {
				for(SupplyPlanVersionDetail resData : resdataList) {
					if(resData.getId().equals(obj.getId())) {
						cleanPlanData(resData);
					}
				}
			}
		}
		
		for(String str : actualIds) {
			List<SupplyPlanVersionDetail> list = getChildData(resdataList,str);
			for(SupplyPlanVersionDetail obj : list) {
				for(SupplyPlanVersionDetail resData : resdataList) {
					if(resData.getId().equals(obj.getId())) {
						cleanActualData(resData);
					}
				}
			}
		}
		
		for(String str : yearOverIds) { 
			List<SupplyPlanVersionDetail> list = getChildData(resdataList,str);
			for(SupplyPlanVersionDetail obj : list) {
				for(SupplyPlanVersionDetail resData : resdataList) {
					if(resData.getId().equals(obj.getId())) {
						cleanPlanData(resData);
					}
				}
			}
		}
		
		for(String str : monthOverIds) { 
			List<SupplyPlanVersionDetail> list = getChildData(resdataList,str);
			for(SupplyPlanVersionDetail obj : list) {
				for(SupplyPlanVersionDetail resData : resdataList) {
					if(resData.getId().equals(obj.getId())) {
						cleanPlanData(resData);
					}
				}
			}
		}
		
	}
	/**
	 * 
	 * @Description 获取当前及以下层级
	 * @author：yyd
	 * @date：2020年4月14日 下午12:17:07
	 */
	private List<SupplyPlanVersionDetail> getSelfAndChildData(List<SupplyPlanVersionDetail> list, String orgFullPath) {
		List<SupplyPlanVersionDetail> resList = Collections.synchronizedList(new ArrayList<>());
		if(CollectionUtils.isNotEmpty(list)) {
			for(SupplyPlanVersionDetail detail : list) {
				String type = detail.getType();
				if(type.equals(OrgTypeEunm.ORG_NOT_SALING_PARKING.getKey())) {
					continue;
				}
				if(detail.getOrgFullPathName().contains(orgFullPath)) {
					resList.add(detail);
				}
			}
		}
		return resList;
	}
	
	/**
	 * 
	 * @Description 获取所有子级
	 * @author：yyd
	 * @date：2020年4月14日 下午12:17:07
	 */
	private List<SupplyPlanVersionDetail> getChildData(List<SupplyPlanVersionDetail> list,String id) {
		List<SupplyPlanVersionDetail> childList = Collections.synchronizedList(new ArrayList<>());
		if(CollectionUtils.isNotEmpty(list)) {
			for(SupplyPlanVersionDetail detail : list) {
				if(detail.getPid().equals(id) || detail.getId().equals(id)) {
					childList.add(detail);
				}
			}
		}
		return childList;
	}
	
	/**
	 * 
	 * @Description 获取所有父级
	 * @author：yyd
	 * @date：2020年4月14日 下午12:17:07
	 */
	public List<SupplyPlanVersionDetail> getParentList(List<SupplyPlanVersionDetail> allList,String id){
		if(CollectionUtils.isNotEmpty(allList)) {
			for(SupplyPlanVersionDetail allDetail : allList) {
				if(allDetail.getId().equals(id)) {
					getParentList(allList,allDetail.getPid());
					parentList.add(allDetail);
				}
			}
		}
		return parentList;
	}
	
	/**
	 * 
	 * @Description 判断是否存在子节点
	 * @return boolean
	 * @author：yyd
	 * @date：2020年4月14日 下午12:17:07
	 */
	private boolean hasChild(List<SupplyPlanVersionDetail> list,String id) {
		boolean flag = false;
		for(SupplyPlanVersionDetail supplyPlanVersionDetail : list) {
			String pid = supplyPlanVersionDetail.getPid();
			if(pid.equals(id)) {
				flag = true;
				break;
			}
		}
		return  flag;
	}
	
	SumValueVO sumVo = new SumValueVO(); 
	Map<String, SumValueVO> baseSumMap = new HashMap<>();
	private List<SupplyPlanVersionDetail> collectData(List<SupplyPlanVersionDetail> list,String versionType) {
		//1、数据汇总
		for(SupplyPlanVersionDetail supplyPlanVersionDetail : list) {
			String id = supplyPlanVersionDetail.getId();
			boolean hasChild = hasChild(list,id);
			if(hasChild) {
				SumValueVO sum = buildDataTree(list,id,new SumValueVO());
				baseSumMap.put(id,sum);
				sumVo = new SumValueVO();
			}
		}
		
		//2、保存汇总的值
		for(Map.Entry<String,SumValueVO> entry : baseSumMap.entrySet()) {
			 String key = entry.getKey();
			 SumValueVO value = entry.getValue();
			 for(SupplyPlanVersionDetail supplyPlanVersionDetail : list) {
	            	if(supplyPlanVersionDetail.getId().equals(key)) {
	            		//面积
	            		supplyPlanVersionDetail.setSaleArea(value.getSumArea());
	            		//套数（非车位/车位）
	            		supplyPlanVersionDetail.setRoomNum(value.getSumRoomNum());
	            		supplyPlanVersionDetail.setParkingNum(value.getSumParkingNum());
	            		//金额（非车位/车位）
	            		supplyPlanVersionDetail.setHouseCommodityValue(value.getSumHouseCommodityValue());
	            		supplyPlanVersionDetail.setNotCommodityValue(value.getSumNotCommodityValue());
	            		supplyPlanVersionDetail.setCommodityValue(value.getSumValue());
	            		
	            		supplyPlanVersionDetail.setAreaActual(value.getSumAreaActual());
	            		supplyPlanVersionDetail.setRoomNumActual(value.getSumRoomNumActual());
	            		supplyPlanVersionDetail.setParkingNumActual(value.getSumParkingNumActual());
	            		supplyPlanVersionDetail.setHouseCommodityValueActual(value.getSumHouseCommodityValueActual());
	            		supplyPlanVersionDetail.setNotCommodityValueActual(value.getSumNotCommodityValueActual());
	            		supplyPlanVersionDetail.setCommodityValueActual(value.getSumValueActual());
	            	}
	            }
		}
		
		//3、处理前端显示的值
		if(versionType.equals(VersionTypeEunm.INVESTMENT.getKey())) {
			beforeWarplanView(list);
		}else {
			//判断动态版基于版本是战规前还是战规后
			if(isBeforeWarplan(list)) {
				beforeWarplanView(list);
			}else {
				afterWarplanView(list);
			}
		}
		return list;
	}
	
	/**
	 * 
	 * 方法描述：战规后页面显示数据处理
	 * 创建人：yyd
	 * 创建时间：2019年11月9日 上午10:53:52
	 */
	private void afterWarplanView(List<SupplyPlanVersionDetail> list) {
		for(SupplyPlanVersionDetail supplyPlanVersionDetail : list) {
			String type = supplyPlanVersionDetail.getType();
			Integer isParking = supplyPlanVersionDetail.getIsParking();
			if(!type.equals(OrgTypeEunm.ORG_BUILD.getKey()) ) {
				supplyPlanVersionDetail.setIsSupply(null);
			}
			if(type.equals(OrgTypeEunm.ORG_STAGE.getKey()) || type.equals(OrgTypeEunm.ORG_GROUP.getKey())) {
				supplyPlanVersionDetail.setNums(supplyPlanVersionDetail.getRoomNum()+" / "+supplyPlanVersionDetail.getParkingNum());
				supplyPlanVersionDetail.setPrice(NumberUtils.BigDecimalADD(4,supplyPlanVersionDetail.getHouseCommodityValue(),supplyPlanVersionDetail.getNotCommodityValue())+" / "+supplyPlanVersionDetail.getHouseCommodityValue()+" / "+supplyPlanVersionDetail.getNotCommodityValue());
				supplyPlanVersionDetail.setAvgPrice(StrUtils.divide(supplyPlanVersionDetail.getHouseCommodityValue()+"",supplyPlanVersionDetail.getSaleArea()+"",2)+" / "+ StrUtils.divide(supplyPlanVersionDetail.getNotCommodityValue()+"",supplyPlanVersionDetail.getParkingNum()+"",2));
				if(supplyPlanVersionDetail.getSaleArea() == null) {
					supplyPlanVersionDetail.setSaleArea(BigDecimal.ZERO);
				}
				
				//实际
				supplyPlanVersionDetail.setNumsActual(supplyPlanVersionDetail.getRoomNumActual()+" / "+supplyPlanVersionDetail.getParkingNumActual());
				supplyPlanVersionDetail.setPriceActual(NumberUtils.BigDecimalADD(4,supplyPlanVersionDetail.getHouseCommodityValueActual(),supplyPlanVersionDetail.getNotCommodityValueActual())+" / "+supplyPlanVersionDetail.getHouseCommodityValueActual()+" / "+supplyPlanVersionDetail.getNotCommodityValueActual());
				supplyPlanVersionDetail.setAvgPriceActual(StrUtils.divide(supplyPlanVersionDetail.getHouseCommodityValueActual()+"",supplyPlanVersionDetail.getAreaActual()+"",2)+" / "+ StrUtils.divide(supplyPlanVersionDetail.getNotCommodityValueActual()+"",supplyPlanVersionDetail.getParkingNumActual()+"",2));
				if(supplyPlanVersionDetail.getAreaActual() == null) {
					supplyPlanVersionDetail.setAreaActual(BigDecimal.ZERO);
				}
				
				//达成率=实际货值/计划货值
				BigDecimal sumPriceActual = NumberUtils.BigDecimalADD(3,supplyPlanVersionDetail.getHouseCommodityValueActual(),supplyPlanVersionDetail.getNotCommodityValueActual());
				BigDecimal sumPrice =NumberUtils.BigDecimalADD(3,supplyPlanVersionDetail.getHouseCommodityValue(),supplyPlanVersionDetail.getNotCommodityValue());
				
				if(sumPrice == null || sumPrice.compareTo(BigDecimal.ZERO) == 0) {
					supplyPlanVersionDetail.setValueAchievingRate("-");
				}else {
					supplyPlanVersionDetail.setValueAchievingRate(StrUtils.divide(sumPriceActual+"",sumPrice+"",4));
				}
			}else {
				if(isParking != null && isParking.intValue() == 1) {
					if(type.equals(OrgTypeEunm.ORG_NOT_SALING_PARKING.getKey())) {
						supplyPlanVersionDetail.setNums(supplyPlanVersionDetail.getForsaleParkingNum()+"");
						supplyPlanVersionDetail.setAvgPrice(supplyPlanVersionDetail.getAveragePriceParkingForsale()+"");
						supplyPlanVersionDetail.setPrice(supplyPlanVersionDetail.getCommodityValue()+"");
						supplyPlanVersionDetail.setSaleArea(null);
						//实际
						supplyPlanVersionDetail.setNumsActual(supplyPlanVersionDetail.getForsaleParkingNumActual()+"");
						supplyPlanVersionDetail.setAvgPriceActual(supplyPlanVersionDetail.getAveragePriceParkingForsaleActual()+"");
						supplyPlanVersionDetail.setPriceActual(supplyPlanVersionDetail.getCommodityValueActual()+"");
						supplyPlanVersionDetail.setAreaActual(null);
						//达成率=实际货值/计划货值
						if(supplyPlanVersionDetail.getCommodityValue() == null || supplyPlanVersionDetail.getCommodityValue().compareTo(BigDecimal.ZERO) == 0) {
							supplyPlanVersionDetail.setValueAchievingRate("-");
						}else {
							supplyPlanVersionDetail.setValueAchievingRate(StrUtils.divide(supplyPlanVersionDetail.getCommodityValueActual()+"",supplyPlanVersionDetail.getCommodityValue()+"",4));
						}
					}else {
						supplyPlanVersionDetail.setNums(supplyPlanVersionDetail.getParkingNum()+"");
						supplyPlanVersionDetail.setSaleArea(null);
						supplyPlanVersionDetail.setAvgPrice(StrUtils.divide(supplyPlanVersionDetail.getNotCommodityValue()+"",supplyPlanVersionDetail.getParkingNum()+"",2));
						supplyPlanVersionDetail.setPrice(supplyPlanVersionDetail.getNotCommodityValue()+"");
						//实际
						supplyPlanVersionDetail.setNumsActual(supplyPlanVersionDetail.getParkingNumActual()+"");
						supplyPlanVersionDetail.setAreaActual(null);
						supplyPlanVersionDetail.setAvgPriceActual(StrUtils.divide(supplyPlanVersionDetail.getNotCommodityValueActual()+"",supplyPlanVersionDetail.getParkingNumActual()+"",2));
						supplyPlanVersionDetail.setPriceActual(supplyPlanVersionDetail.getNotCommodityValueActual()+"");
						//达成率=实际货值/计划货值
						if(supplyPlanVersionDetail.getNotCommodityValue() == null || supplyPlanVersionDetail.getNotCommodityValue().compareTo(BigDecimal.ZERO) == 0) {
							supplyPlanVersionDetail.setValueAchievingRate("-");
						}else {
							supplyPlanVersionDetail.setValueAchievingRate(StrUtils.divide(supplyPlanVersionDetail.getNotCommodityValueActual()+"",supplyPlanVersionDetail.getNotCommodityValue()+"",4));
						}
					} 
				}else {
					supplyPlanVersionDetail.setNums(supplyPlanVersionDetail.getRoomNum()+"");
					supplyPlanVersionDetail.setPrice(supplyPlanVersionDetail.getHouseCommodityValue()+"");
					supplyPlanVersionDetail.setAvgPrice(StrUtils.divide(supplyPlanVersionDetail.getHouseCommodityValue()+"",supplyPlanVersionDetail.getSaleArea()+"",2));
					if(supplyPlanVersionDetail.getSaleArea() == null) {
						supplyPlanVersionDetail.setSaleArea(BigDecimal.ZERO);
					}
					//实际
					supplyPlanVersionDetail.setNumsActual(supplyPlanVersionDetail.getRoomNumActual()+"");
					supplyPlanVersionDetail.setPriceActual(supplyPlanVersionDetail.getHouseCommodityValueActual()+"");
					supplyPlanVersionDetail.setAvgPriceActual(StrUtils.divide(supplyPlanVersionDetail.getHouseCommodityValueActual()+"",supplyPlanVersionDetail.getAreaActual()+"",2));
					if(supplyPlanVersionDetail.getAreaActual() == null) {
						supplyPlanVersionDetail.setAreaActual(BigDecimal.ZERO);
					}
					//达成率=实际货值/计划货值
					if(supplyPlanVersionDetail.getHouseCommodityValue() == null || supplyPlanVersionDetail.getHouseCommodityValue().compareTo(BigDecimal.ZERO) == 0) {
						supplyPlanVersionDetail.setValueAchievingRate("-");
					}else {
						supplyPlanVersionDetail.setValueAchievingRate(StrUtils.divide(supplyPlanVersionDetail.getHouseCommodityValueActual()+"",supplyPlanVersionDetail.getHouseCommodityValue()+"",4));
					}
				}
			}
		}
	}

	/**
	 * 
	 * 方法描述：战规前页面显示数据处理
	 * 创建人：yyd
	 * 创建时间：2019年11月9日 上午10:53:52
	 */
	private void beforeWarplanView(List<SupplyPlanVersionDetail> list) {
		for(SupplyPlanVersionDetail supplyPlanVersionDetail : list) {
			String type = supplyPlanVersionDetail.getType();
			Integer isParking = supplyPlanVersionDetail.getIsParking();
			if(!type.equals(OrgTypeEunm.ORG_PRODUCT.getKey()) ) {
				supplyPlanVersionDetail.setIsSupply(null);
			}
			if(type.equals(OrgTypeEunm.ORG_STAGE.getKey()) || type.equals(OrgTypeEunm.ORG_GROUP.getKey()) || type.equals(OrgTypeEunm.ORG_BATCH.getKey())) {
				supplyPlanVersionDetail.setNums(supplyPlanVersionDetail.getRoomNum()+" / "+supplyPlanVersionDetail.getParkingNum());
				supplyPlanVersionDetail.setAvgPrice(StrUtils.divide(supplyPlanVersionDetail.getHouseCommodityValue()+"",supplyPlanVersionDetail.getSaleArea()+"",2)+" / "+ StrUtils.divide(supplyPlanVersionDetail.getNotCommodityValue()+"",supplyPlanVersionDetail.getParkingNum()+"",2));
				supplyPlanVersionDetail.setPrice(NumberUtils.BigDecimalADD(4,supplyPlanVersionDetail.getHouseCommodityValue(),supplyPlanVersionDetail.getNotCommodityValue())+" / "+supplyPlanVersionDetail.getHouseCommodityValue()+" / "+supplyPlanVersionDetail.getNotCommodityValue());
				if(supplyPlanVersionDetail.getSaleArea() == null) {
					supplyPlanVersionDetail.setSaleArea(BigDecimal.ZERO);
				}
				supplyPlanVersionDetail.setValueAchievingRate("-");
			}else {
				if(isParking != null && isParking.intValue() == 1) {
					if(type.equals(OrgTypeEunm.ORG_NOT_SALING_PARKING.getKey())) {
						supplyPlanVersionDetail.setNums(supplyPlanVersionDetail.getForsaleParkingNum()+"");
						supplyPlanVersionDetail.setSaleArea(null);
						supplyPlanVersionDetail.setPrice(supplyPlanVersionDetail.getCommodityValue()+"");
						supplyPlanVersionDetail.setAvgPrice(supplyPlanVersionDetail.getAveragePriceParkingForsale()+"");
					}else {
						supplyPlanVersionDetail.setNums(supplyPlanVersionDetail.getParkingNum()+"");
						supplyPlanVersionDetail.setSaleArea(null);
						supplyPlanVersionDetail.setPrice(supplyPlanVersionDetail.getNotCommodityValue()+"");
						supplyPlanVersionDetail.setAvgPrice(StrUtils.divide(supplyPlanVersionDetail.getNotCommodityValue()+"",supplyPlanVersionDetail.getParkingNum()+"",2));
					}
					supplyPlanVersionDetail.setValueAchievingRate("-");
				}else {
					supplyPlanVersionDetail.setNums(supplyPlanVersionDetail.getRoomNum()+"");
					supplyPlanVersionDetail.setAvgPrice(StrUtils.divide(supplyPlanVersionDetail.getHouseCommodityValue()+"",supplyPlanVersionDetail.getSaleArea()+"",2));
					supplyPlanVersionDetail.setPrice(supplyPlanVersionDetail.getHouseCommodityValue()+"");
					if(supplyPlanVersionDetail.getSaleArea() == null) {
						supplyPlanVersionDetail.setSaleArea(BigDecimal.ZERO);
					}
					supplyPlanVersionDetail.setValueAchievingRate("-");
				}
			}
		}
	}
	
	/**
	 * 
	 * 方法描述：判断动态版本基于的版本是战规前还是战规后
	 * 创建人：yyd
	 * 创建时间：2019年11月9日 上午10:51:01
	 */
	private boolean isBeforeWarplan(List<SupplyPlanVersionDetail> list) {
		for(SupplyPlanVersionDetail obj : list) {
			if(obj.getType().equals(OrgTypeEunm.ORG_BATCH.getKey())) {
				return true;
			}
		}
		return false;
	}

	private SumValueVO buildDataTree(List<SupplyPlanVersionDetail> list, String parentId,SumValueVO vo) {
		for(SupplyPlanVersionDetail supplyPlanVersionDetail : list) {
			String id = supplyPlanVersionDetail.getId();
			String pid = supplyPlanVersionDetail.getPid();
			String businessTypeName = supplyPlanVersionDetail.getBusinessTypeName();
			Integer isParking = supplyPlanVersionDetail.getIsParking();
			
			//计划
			BigDecimal area = supplyPlanVersionDetail.getSaleArea()== null?BigDecimal.ZERO:supplyPlanVersionDetail.getSaleArea();
			Integer roomNum = supplyPlanVersionDetail.getRoomNum()== null?0:supplyPlanVersionDetail.getRoomNum();
			Integer parkingNum = supplyPlanVersionDetail.getParkingNum()== null?0:supplyPlanVersionDetail.getParkingNum();
			BigDecimal houseCommodityValue = supplyPlanVersionDetail.getHouseCommodityValue() == null?BigDecimal.ZERO:supplyPlanVersionDetail.getHouseCommodityValue();
			BigDecimal notCommodityValue = supplyPlanVersionDetail.getNotCommodityValue() == null?BigDecimal.ZERO:supplyPlanVersionDetail.getNotCommodityValue();
			BigDecimal commodityValue = supplyPlanVersionDetail.getCommodityValue() == null ? BigDecimal.ZERO:supplyPlanVersionDetail.getCommodityValue();
			
			//实际
			BigDecimal areaActual = supplyPlanVersionDetail.getAreaActual()==null?BigDecimal.ZERO:supplyPlanVersionDetail.getAreaActual();
			Integer roomNumActual = supplyPlanVersionDetail.getRoomNumActual()==null?0:supplyPlanVersionDetail.getRoomNumActual();
			Integer parkingNumActual =supplyPlanVersionDetail.getParkingNumActual()==null?0:supplyPlanVersionDetail.getParkingNumActual();
			BigDecimal houseCommodityValueActual = supplyPlanVersionDetail.getHouseCommodityValueActual()==null?BigDecimal.ZERO:supplyPlanVersionDetail.getHouseCommodityValueActual();
			BigDecimal notCommodityValueActual = supplyPlanVersionDetail.getNotCommodityValueActual()==null?BigDecimal.ZERO:supplyPlanVersionDetail.getNotCommodityValueActual();
			BigDecimal commodityValueActual = supplyPlanVersionDetail.getCommodityValueActual() == null ? BigDecimal.ZERO:supplyPlanVersionDetail.getCommodityValueActual();
			//车位数据 可售面积不累加
			if(isParking!= null && isParking.intValue() == 1) {
				area = BigDecimal.ZERO;
				areaActual = BigDecimal.ZERO;
			}
			if(parentId.equals(pid)) {

				if(StrUtils.isEmpty(businessTypeName) || isValid(businessTypeName)) {
					//计划
					BigDecimal sumArea = area.add(vo.getSumArea());
					Integer sumRoomNum = roomNum + vo.getSumRoomNum();
					Integer sumParkingNum = parkingNum + vo.getSumParkingNum();
					BigDecimal sumHouseCommodityValue = houseCommodityValue.add(vo.getSumHouseCommodityValue());
					BigDecimal sumNotCommodityValue = notCommodityValue.add(vo.getSumNotCommodityValue());
					BigDecimal sumCommodityValue = commodityValue.add(vo.getSumValue());
				
					//实际
					BigDecimal sumAreaActual = areaActual.add(vo.getSumAreaActual());
					Integer sumRoomNumActual = roomNumActual + vo.getSumRoomNumActual();
					Integer sumParkingNumActual = parkingNumActual + vo.getSumParkingNumActual();
					BigDecimal sumHouseCommodityValueActual = houseCommodityValueActual.add(vo.getSumHouseCommodityValueActual());
					BigDecimal sumNotCommodityValueActual = notCommodityValueActual.add(vo.getSumNotCommodityValueActual());
					BigDecimal sumCommodityValueActual = commodityValueActual.add(vo.getSumValueActual());
					
					//计划
					vo.setSumArea(sumArea);
					vo.setSumRoomNum(sumRoomNum);
					vo.setSumParkingNum(sumParkingNum);
					vo.setSumHouseCommodityValue(sumHouseCommodityValue);
					vo.setSumNotCommodityValue(sumNotCommodityValue);
					vo.setSumValue(sumCommodityValue);
					//实际
					vo.setSumAreaActual(sumAreaActual);
					vo.setSumRoomNumActual(sumRoomNumActual);
					vo.setSumParkingNumActual(sumParkingNumActual);
					vo.setSumHouseCommodityValueActual(sumHouseCommodityValueActual);
					vo.setSumNotCommodityValueActual(sumNotCommodityValueActual);
					vo.setSumValueActual(sumCommodityValueActual);
					buildDataTree(list,id,vo);
					
					//计划
					sumVo.setSumArea(vo.getSumArea());
					sumVo.setSumRoomNum(vo.getSumRoomNum());
					sumVo.setSumParkingNum(vo.getSumParkingNum());
					sumVo.setSumHouseCommodityValue(vo.getSumHouseCommodityValue());
					sumVo.setSumNotCommodityValue(vo.getSumNotCommodityValue());
					sumVo.setSumValue(vo.getSumValue());
					//实际
					sumVo.setSumAreaActual(vo.getSumAreaActual());
					sumVo.setSumRoomNumActual(vo.getSumRoomNumActual());
					sumVo.setSumParkingNumActual(vo.getSumParkingNumActual());
					sumVo.setSumHouseCommodityValueActual(vo.getSumHouseCommodityValueActual());
					sumVo.setSumNotCommodityValueActual(vo.getSumNotCommodityValueActual());
					sumVo.setSumValueActual(vo.getSumValueActual());
				}
			}
		}
		return sumVo;
	}
	
	
	private boolean isValid(String businessTypeName) {
		return businessTypeName.equals(SalyTypeEnum.SALABLE.getValue()) || businessTypeName.equals(SalyTypeEnum.STAGE_SELF_SUSTAINMENT.getValue());
	}

	private boolean isShow(List<SupplyPlanVersionDetail> list,String id) {
		boolean flag = false;
		for(SupplyPlanVersionDetail supplyPlanVersionDetail : list) {
			String pid = supplyPlanVersionDetail.getPid();
			String businessTypeName = supplyPlanVersionDetail.getBusinessTypeName();
			if(pid.equals(id)) {
				if(StrUtils.isEmpty(businessTypeName) || isValid(businessTypeName)) {
					flag = true;
					break;
				}
			}
		}
		return  flag;
	}
	
	//处理显示数据（经营方式为可租和不可租售数据不显示）
	private void handleShowData(List<SupplyPlanVersionDetail> resdataList,List<SupplyPlanVersionDetail> lastList) {
		for(SupplyPlanVersionDetail supplyPlanVersionDetail : resdataList) {
			String businessTypeName = supplyPlanVersionDetail.getBusinessTypeName();
			if(StrUtils.isEmpty(businessTypeName)) {
				//判断是否有子级
				boolean flag = hasChild(resdataList,supplyPlanVersionDetail.getId());
				if(flag) {
					//子级经营方式是否都是可租或不可租售 ，是不显示，不是显示
					if(isShow(resdataList,supplyPlanVersionDetail.getId())) {
						lastList.add(supplyPlanVersionDetail);
					}
				}
			}else if(businessTypeName.equals(SalyTypeEnum.SALABLE.getValue()) || businessTypeName.equals(SalyTypeEnum.STAGE_SELF_SUSTAINMENT.getValue())){
				lastList.add(supplyPlanVersionDetail);
			}
		}
	}
	
	private List<SupplyPlanVersionDetail> getTempList(List<SupplyPlanVersionDetail> allList) {
		if(CollectionUtils.isEmpty(allList)) {
			return null;
		}
		List<SupplyPlanVersionDetail> dataList = Collections.synchronizedList(new ArrayList<>());
		for(SupplyPlanVersionDetail de : allList) {
			try {
				dataList.add((SupplyPlanVersionDetail) BeanUtils.cloneBean(de));
			} catch (Exception e) {
				log.error("#### 数据转换异常", e);
			}
		}
		return dataList;
	}
	
	private void emptyData(List<SupplyPlanVersionDetail> resdataList) {
		for(SupplyPlanVersionDetail obj : resdataList) {
			if(hasChild(resdataList,obj.getId())) {
				cleanPlanData(obj);
				cleanActualData(obj);
			}
		}
	}
	
	private void cleanActualData(SupplyPlanVersionDetail resData) {
		resData.setRoomNumActual(null);
		resData.setParkingNumActual(null);
		resData.setAreaActual(null);
		resData.setAveragePriceHouseActual(null);
		resData.setAveragePriceParkingActual(null);
		resData.setHouseCommodityValueActual(null);
		resData.setNotCommodityValueActual(null);
		resData.setCommodityValueActual(null);
	}
	
	private void cleanPlanData(SupplyPlanVersionDetail resData) {
		resData.setNum(null);
		resData.setRoomNum(null);
		resData.setParkingNum(null);
		resData.setCommodityValue(null);
		resData.setHouseCommodityValue(null);
		resData.setNotCommodityValue(null);
		resData.setAveragePrice(null);
		resData.setAveragePriceParking(null);
		resData.setAveragePriceHouse(null);
		resData.setSaleArea(null);
	}
}
